package com.liujit.pay.wx.domain.params.order;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.liujit.pay.wx.domain.MyPayInfo;
import lombok.Builder;

/**
 * @description: JSAPI支付/小程序支付统一下单请求参数
 * @author: liujun
 * @create: 2021-02-19 16:39
 */
@Builder
public class JSAPIOrderParam extends MyPayInfo {

    /**
     * 商品描述
     */
    private String description;

    /**
     * 商户订单号
     */
    private String outTradeNo;

    /**
     * 交易结束时间
     */
    private String timeExpire;

    /**
     * 附加数据
     */
    private String attach;

    /**
     * 通知地址
     */
    private String notifyUrl;

    /**
     * 订单优惠标记
     */
    private String goodsTag;

    /**
     * 订单金额
     */
    private OrderAmount orderAmount;

    /**
     * 支付者
     */
    private OrderPayer orderPayer;

    /**
     * 优惠功能
     */
    private OrderDetail orderDetail;

    /**
     * 场景信息
     */
    private OrderSceneInfo orderSceneInfo;

    JSAPIOrderParam(String description, String outTradeNo, String timeExpire, String attach, String notifyUrl,
                    String goodsTag, OrderAmount orderAmount, OrderPayer orderPayer, OrderDetail orderDetail,
                    OrderSceneInfo orderSceneInfo) {
        this.description = description;
        this.outTradeNo = outTradeNo;
        this.timeExpire = timeExpire;
        this.attach = attach;
        this.notifyUrl = notifyUrl;
        this.goodsTag = goodsTag;
        this.orderAmount = orderAmount;
        this.orderPayer = orderPayer;
        this.orderDetail = orderDetail;
        this.orderSceneInfo = orderSceneInfo;
    }

    public static JSAPIOrderParam.Builder builder(String description, String outTradeNo, String notifyUrl, OrderAmount orderAmount, OrderPayer orderPayer) {
        return new JSAPIOrderParam.Builder(description, outTradeNo, notifyUrl, orderAmount, orderPayer);
    }

    public static class Builder {

        private String description;
        private String outTradeNo;
        private String timeExpire;
        private String attach;
        private String notifyUrl;
        private String goodsTag;
        private OrderAmount orderAmount;
        private OrderPayer orderPayer;
        private OrderDetail orderDetail;
        private OrderSceneInfo orderSceneInfo;

        Builder(String description, String outTradeNo, String notifyUrl, OrderAmount orderAmount, OrderPayer orderPayer) {

            this.description = description;
            this.outTradeNo = outTradeNo;
            this.notifyUrl = notifyUrl;
            this.orderAmount = orderAmount;
            this.orderPayer = orderPayer;
        }

        public JSAPIOrderParam.Builder timeExpire(String timeExpire) {
            this.timeExpire = timeExpire;
            return this;
        }

        public JSAPIOrderParam.Builder attach(String attach) {
            this.attach = attach;
            return this;
        }

        public JSAPIOrderParam.Builder goodsTag(String goodsTag) {
            this.goodsTag = goodsTag;
            return this;
        }

        public JSAPIOrderParam.Builder orderDetail(OrderDetail orderDetail) {
            this.orderDetail = orderDetail;
            return this;
        }

        public JSAPIOrderParam.Builder orderSceneInfo(OrderSceneInfo orderSceneInfo) {
            this.orderSceneInfo = orderSceneInfo;
            return this;
        }

        public JSAPIOrderParam build() {
            return new JSAPIOrderParam(this.description, this.outTradeNo, this.timeExpire, this.attach, this.notifyUrl,
                    this.goodsTag, this.orderAmount, this.orderPayer, this.orderDetail, this.orderSceneInfo);
        }
    }

    /**
     * 转化JSON字符串
     *
     * @return
     */
    public String toJSONString() {
        JSONObject json = new JSONObject();
        // 应用ID
        json.set("appid", super.getAppId());
        // 直连商户号
        json.set("mchid", super.getMchId());
        // 商品描述
        json.set("description", description);
        // 商户订单号
        json.set("out_trade_no", outTradeNo);
        // 交易结束时间
        if (StrUtil.isNotBlank(timeExpire)) {
            json.set("time_expire", timeExpire);
        }
        // 附加数据
        if (StrUtil.isNotBlank(attach)) {
            json.set("attach", attach);
        }
        // 通知地址
        json.set("notify_url", notifyUrl);
        // 订单优惠标记
        if (StrUtil.isNotBlank(goodsTag)) {
            json.set("goods_tag", goodsTag);
        }
        // 订单金额
        if (orderAmount != null) {
            json.set("amount", orderAmount.toJSONString());
        }
        // 支付者
        if (orderPayer != null) {
            json.set("payer", orderPayer.toJSONString());
        }
        // 优惠功能
        if (orderDetail != null) {
            json.set("detail", orderDetail.toJSONString());
        }
        // 场景信息
        if (orderSceneInfo != null) {
            json.set("scene_info", orderSceneInfo.toJSONString());
        }
        return json.toString();
    }
}
